package com.fragrance.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fragrance.service.UserService;
import com.fragrance.service.impl.UserServiceImpl;
import com.fragrance.util.Encrypt;
import com.jfinal.core.Controller;
import com.jfinal.core.JFinal;

public class UserOpenController  extends Controller {

	private Logger logger = LoggerFactory.getLogger(UserOpenController.class);
	
	private static UserService userService = new UserServiceImpl();
	
	/**
	 * http://127.0.0.1/web/api/open/back/omsLogin?account=A10002&password=E10ADC3949BA59ABBE56E057F20F883E
	 * 管理后台登陆
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void omsLogin() {
		logger.info("-- begin UserOpenController.login===========================");
		Map returnMap = new HashMap();
		try {
			
			//获取参数
			String account = getPara("account");
			String password = getPara("password");
			
			if(StringUtils.isBlank(account)){
				returnMap.put("code", "000005");
				returnMap.put("msg", "账号或密码不能为空");
				renderJson(returnMap);
				return;
			}
			if(StringUtils.isBlank(password)){
				returnMap.put("code", "000005");
				returnMap.put("msg", "账号或密码不能为空");
				renderJson(returnMap);
				return;
			}
			
			Map<String, Object> map = new HashMap<String,Object>();
			map.put("account", account);
			map.put("password",password);

			logger.info("-- in UserOpenController.login,params{}",map);
			returnMap = userService.omsLogin(map);
			logger.info("-- in UserOpenController.login,result{}",returnMap);
			
		} catch (Exception e) {
			logger.error("-- error UserOpenController.login,Exception{}",e);
			returnMap.put("code", "000001");
			returnMap.put("msg", "系统繁忙，请稍后再试！");
		}
		logger.info("-- end UserOpenController.login===========================");
		renderJson(returnMap);
	}
	
	public static void main(String[] args) {
		System.out.println(Encrypt.MD5("123456").toUpperCase());
		// eclipse 下的启动方式
		JFinal.start("src/main/webapp", 80, "/", 5);
		
		// IDEA 下的启动方式
		// JFinal.start("src/main/webapp", 80, "/");
		
	}
}
